//
//  PictureBrowserController.swift
//  WeiBlog
//
//  Created by xiang on 15/10/27.
//  Copyright © 2015年 xiang. All rights reserved.
//

import UIKit

private let PictureBrowserCellID = "PictureBrowserCellID"

/// 图片浏览控制器
class PictureBrowserController: UIViewController {
    
    ///图片URL数组
    private var pictureURLs:[NSURL]
    ///点击的显示的cell的indexPath
    private var indexPath:NSIndexPath
    
    init(pictureURLs:[NSURL],indexPath:NSIndexPath){
        self.pictureURLs = pictureURLs
        self.indexPath = indexPath
        super.init(nibName: nil, bundle: nil)
        //初始化拿到图片URL数组后,就能布局和显示了(第一步)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func loadView() {
        
        let size = UIScreen.mainScreen().bounds.size
        //在这里,就把view拓宽20的距离,是为了两张照片在切换时,有个间距,其次分页时,不会被这个间距影响到
        let rect = CGRect(x: 0, y: 0, width: size.width + 20, height: size.height)
        
        view = UIView(frame: rect)
        
        setUI()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.dataSource = self
        
        self.collectionView.registerClass(PictureBrowserCell.self, forCellWithReuseIdentifier: PictureBrowserCellID)
        
        //直接滚动到点击的图片
        collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
    }

    private class CustomLayout:UICollectionViewFlowLayout{
        private override func prepareLayout() {
            super.prepareLayout()
            
            //在这里还可以改变collectionView的frame
            
            //问题:collectionView是参考谁的collectionView?
            itemSize = collectionView!.bounds.size

            //分页是计算collectionView的大小,view是多大,分页就移动多少距离,
            collectionView?.pagingEnabled = true
            collectionView?.bounces = false
            scrollDirection = .Horizontal
            minimumInteritemSpacing = 0
            minimumLineSpacing = 0
            
        }
    }
    
    // MARK:---懒加载
    private lazy var saveButton:UIButton = UIButton(title: "保存", fontSize: 17, color: UIColor.whiteColor(), bgImageName: nil, imageName: nil, actionName: "saveClick", target: self)
    private lazy var closeButton:UIButton = UIButton(title: "关闭", fontSize: 17, color: nil, bgImageName: nil, imageName: nil, actionName: "closeClick", target: self)
    private lazy var collectionView:UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: CustomLayout())
    
    // MARK:---监听方法
    
    @objc private func saveClick(){
        
        let cell = collectionView.visibleCells()[0] as! PictureBrowserCell
        
        UIImageWriteToSavedPhotosAlbum(cell.imageView.image!, self, nil, nil)
    }
    
    @objc private func closeClick(){
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    // MARK:---设置界面
    private func setUI(){
        
        view.addSubview(collectionView)
        view.addSubview(closeButton)
        view.addSubview(saveButton)
        
        closeButton.backgroundColor = UIColor.grayColor()
        closeButton.alpha = 0.4
        saveButton.backgroundColor = UIColor.grayColor()
        saveButton.alpha = 0.4
        
        //布局
        collectionView.frame = view.bounds
        
        closeButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(saveButton.snp_right).offset(8)
            make.bottom.equalTo(view.snp_bottom).offset(-8)
            make.width.equalTo(80)
        }
        
        saveButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(view.snp_left).offset(8)
            make.bottom.equalTo(view.snp_bottom).offset(-8)
            make.width.equalTo(80)
        }
    }
}

// MARK: ----数据源,代理
extension PictureBrowserController:UICollectionViewDataSource,UICollectionViewDelegate
{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictureURLs.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureBrowserCellID, forIndexPath: indexPath) as! PictureBrowserCell
        
        cell.picURL = pictureURLs[indexPath.item]
        
        return cell
    }
    
}

extension PictureBrowserController
{
}









